/**
 * 
 */
package org.newdawn.yapgame;

/**
 * Abstract Class representing a process in the game.
 * A process is a piece of code executing some game-code which needs to be updated
 * every frame. Processes are managed by an object implementing {@link IProcessManager}.
 * A process can have a successor which is started immediately after it
 * terminates (if handled by an appropriate process manager)
 * 
 * @author Marko Walther
 */
public abstract class Process 
{	
	/** successor process */
	protected Process next = null;
	/** whether or not process is already terminated */
	protected boolean terminated = false;
	
	/**
	 * Executes process within current frame
	 * @param delta time (in milliseconds) since last frame
	 */
	public abstract void execute( long delta );
	
	/**
	 * @return successor process
	 */
	public Process getNext()
	{
		return next;
	}
	
	/**
	 * Returns whether process is already terminated
	 * @return true or false
	 */
	public boolean isTerminated()
	{
		return terminated;
	}
	
	/**
	 * Sets successor process which is started immediately after this process ended
	 * (usually by a process manager)
	 * @param next successor process
	 * @return successor process given in parameter {@link next}
	 */
	public Process setNext( Process next )
	{
		this.next = next;
		return this.next;
	}
	
	/**
	 * is called (usually by a process manager) when this process is started.
	 * Override to do initialization work.
	 */
	public abstract void onStart();
	
	/**
	 * is called (usually by a process manager) when process is terminated.
	 * Override to free resources.
	 */
	public abstract void onTerminate();
	
	/**
	 * Call this to indicate that this process can be terminated in the next frame
	 * (usually its then terminated by a process manager) .
	 */
	public void terminate() 
	{
		terminated = true;
	}
}
